package top.hyz.hrm.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import top.hyz.hrm.dto.EmailDTO;
import top.hyz.hrm.dto.MessageDTO;

import java.io.IOException;

import static top.hyz.hrm.constant.ThirdConstant.*;

/**
 * @ClassName MqListener
 * @Description MQ消费者
 * @Author 韩雨泽
 * @Date 2021/1/23-19:01
 * @Version 1.0
 */
@Component
@Slf4j
public class MqListener {


    @RabbitListener(queues = QUEUE_NAME_SMS)
    public void messageListener(Channel channel, Message message,@Payload MessageDTO messageDTO) {
        //消费消息
        System.out.println("短信：" + messageDTO.getPhones() +"===内容:" + messageDTO.getContent());
        log.info("短信：" + messageDTO.getPhones() +"===内容:" + messageDTO.getContent());
        //手动签收
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = QUEUE_NAME_EMAIL)
    public void emailListener(Channel channel, Message message,@Payload EmailDTO emailDTO) {
        //消费消息
        System.out.println("邮件：" + emailDTO.getUrl() +"===内容:" + emailDTO.getContent());
        log.info("邮件：" + emailDTO.getUrl() +"===内容:" + emailDTO.getContent());
        //手动签收
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
